//
//  AppDelegate.m
//  UIDemo
//
//  Created by shk on 14/11/5.
//  Copyright (c) 2014年 com.shk. All rights reserved.
//

#import "AppDelegate.h"


@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.backgroundColor = [UIColor whiteColor];
    
    /**
     * UILabel控件的使用
     */
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(50, 50, 100, 30)];
    label.text = @"hello world!";
    label.backgroundColor = [UIColor grayColor];
    label.font = [UIFont systemFontOfSize:18];
    label.textAlignment = NSTextAlignmentCenter;
    label.shadowColor = [UIColor redColor];
    label.shadowOffset = CGSizeMake(-2, 2);
    //[label sizeToFit];//根据内容自适应
    [self.window addSubview:label];
    
    
    
    /**
     * UIButton控件的使用
     */
    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    button.frame = CGRectMake(50, 100, 100, 100);
    [button setTitle:@"normal" forState:UIControlStateNormal];//正常状态显示的字体
    [button setTitle:@"highlighted" forState:UIControlStateHighlighted];//高亮状态时显示的文字
    //    [button setTitle:@"disabled" forState:UIControlStateDisabled];//禁用状态显示的文字
    //    button.enabled = NO;//禁用按钮
    //设置背景图片
    [button setBackgroundImage:[UIImage imageNamed:@"ios_logo"] forState:UIControlStateNormal];
    //设置图片（需要将按钮类型设置为UIButtonTypeCustom）
    //    [button setImage:[UIImage imageNamed:@"ios_logo"] forState:UIControlStateNormal];
    //添加touchUpInside事件
    [button addTarget:self action:@selector(touchUpInside:) forControlEvents:UIControlEventTouchUpInside];
    //添加touchDown事件
    [button addTarget:self action:@selector(touchDown) forControlEvents:UIControlEventTouchDown];
    [self.window addSubview:button];
    
    
    /**
     * UITextField控件的使用
     */
    UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(50, 200, 200, 30)];
    textField.borderStyle = UITextBorderStyleLine;
    textField.placeholder = @"请输入用户名";
//    textField.clearsOnBeginEditing = YES;
//    textField.secureTextEntry = YES;//密码方式
    textField.returnKeyType = UIReturnKeySearch;//定义返回键
    
    //添加右侧控件，如在输入框添加右侧删除按钮
    UIView *clearTextFieldView = [[UIView alloc] initWithFrame:CGRectMake(0, 10, 20, 20)];
    clearTextFieldView.backgroundColor = [UIColor yellowColor];
    textField.rightView = clearTextFieldView;
//    textField.rightViewMode = UITextFieldViewModeWhileEditing;
    //自定义系统键盘，如银行专用软件，输入密码使用自己开发的随机键盘
//    UIView *keyboardTextFieldView = [[UIView alloc] initWithFrame:CGRectMake(0, 10, 320, 200)];
//    clearTextFieldView.backgroundColor = [UIColor yellowColor];
//    textField.inputView = keyboardTextFieldView;
    textField.delegate = self;//添加事件代理
//    [textField becomeFirstResponder];//成为第一响应者
    [self.window addSubview:textField];
    
    
    /**
     * UISlider控件的使用
     */
    UISlider *slider = [[UISlider alloc] initWithFrame:CGRectMake(50, 250, 200, 5)];
    slider.maximumValue = 1;
    slider.minimumValue = 0;
    slider.value = 0.3;
    [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(testSlider:) userInfo:slider repeats:NO];
    [self.window addSubview:slider];
    
    
    
    /**
     * UISlider控件的使用
     */
    NSArray *items = @[@"搜索", @"图片", @"音乐"];
    UISegmentedControl *sc = [[UISegmentedControl alloc] initWithItems:items];
    sc.frame = CGRectMake(50, 300, 200, 30);
    sc.selectedSegmentIndex = 1;
    [self.window addSubview:sc];
    
    
    /**
     * UIPageControl控件的使用，比如：用于相册展示
     */
    UIPageControl *pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(50, 350, 200, 50)];
    pageControl.numberOfPages = 10;
    pageControl.currentPage = 2;
    pageControl.backgroundColor = [UIColor grayColor];
    [self.window addSubview:pageControl];
    
    
    /**
     * UIActivityIndicatorView控件的使用，比如：用于加载中提示
     */
    UIActivityIndicatorView *activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    activityView.center = CGPointMake(160, 200);
    activityView.backgroundColor = [UIColor grayColor];
    [activityView startAnimating];
    [self.window addSubview:activityView];
    
    
    /**
     * UIAlertView控件的使用
     */
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"标题UIAlertView" message:@"我是提示框！" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    [alertView show];
    
    
    /**
     * UIActionSheet控件的使用
     */
    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"标题UIActionSheet" delegate:nil cancelButtonTitle:@"取消" destructiveButtonTitle:@"destructive" otherButtonTitles:@"button1", @"button2", nil];
    [actionSheet showInView:self.window];
    
    
    /**
     * UIImageView控件的使用
     */
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(50, 400, 100, 50)];
    imageView.image = [UIImage imageNamed:@"ios_logo"];
    [self.window addSubview:imageView];
    
    
    
    
    
    
    
    [self.window makeKeyAndVisible];
    return YES;
}



#pragma mark - Button Delegate start

- (void) touchUpInside:(UIButton *) button{
    if(button.selected){
        button.selected = NO;
    }else{
        button.selected = YES;
    }
    NSLog(@"touchUpInside");
}


- (void) touchDown{
    NSLog(@"touchDown");
}


#pragma mark - Button Delegate end


#pragma mark - TextField Delegate start

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    NSLog(@"textFieldShouldBeginEditing");
    return YES;
}

// return NO to disallow editing.
- (void)textFieldDidBeginEditing:(UITextField *)textField{

}

#pragma mark - TextField Delegate end


#pragma mark - Button Delegate start

- (void) testSlider:(NSTimer *) timers{
    UISlider *slider = [timers userInfo];
    [slider setValue:0.5 animated:NO];
}



- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
